const router = require("koa-router")();
const result = require("../../config/result");
const {
  getToken,
  addUrl,
  whereUrl,
  updateUrl,
} = require("../../config/databaseapi");
const { regCheck, shopInfor, categoryCheck } = require("../../config/checking");
const { Auth } = require("../../token/auth");
const { upload, cosfun } = require("../../cos/cos");

//图片上传
router.post("/uploadres", upload.single("file"), async (ctx) => {
  try {
    const res = await cosfun(ctx.file.filename, ctx.file.path);
    new result(ctx, "success", 200, "https://" + res).answer();
  } catch (error) {
    new result(ctx, "上传失败", 500).answer();
  }
});
//商家信息shangch
router.post("/uploadshop", new Auth().m, async (ctx) => {
  const { id, name, address, logo } = ctx.request.body;
  new shopInfor(ctx, name, address, logo).start();
  let query = `db.collection('shop-infor').add({data:{name:'${name}',address:'${address}',logo:${logo}}})`;
  try {
    await new getToken().posteve(addUrl, query);
    new result(ctx, "提交成功", 200).answer();
  } catch (error) {
    new result(ctx, "提交失败", 500).answer();
  }
});
//获取商家信息
router.get("/obtainshop", new Auth().m, async (ctx) => {
  let query = `db.collection('shop-infor').get()`;
  try {
    let res = await new getToken().posteve(whereUrl, query);
    const data = res.data.map((item) => {
      return JSON.parse(item);
    });
    new result(ctx, "查询成功", 200, data).answer();
  } catch (error) {
    new result(ctx, "fail", 500).answer();
  }
});
//更新商家信息
router.post("/modifyshop", new Auth().m, async (ctx) => {
  const { id, name, address, logo } = ctx.request.body;
  new shopInfor(ctx, name, address, logo).start();
  let query = `db.collection('shop-infor').doc('${id}').update({data:{name:'${name}',address:'${address}',logo:${logo}}})`;
  try {
    await new getToken().posteve(updateUrl, query);
    new result(ctx, "修改成功", 200).answer();
  } catch (error) {
    new result(ctx, "修改失败", 500).answer();
  }
});
//添加菜品类目
router.post("/addcategory", new Auth().m, async (ctx) => {
  const { category } = ctx.request.body;
  new categoryCheck(ctx, category).start();
  const cid  ='a'+ new Date().getTime()
  //查询菜品类目是否存在
  let whereQuery = `db.collection('category').where({label:'${category}'}).get()`;
  let query = `db.collection('category').add({data:{value:'${category}',label:'${category}',cid:'${cid}',count:0,sele_quantity:0}})`;
  try {
      let res = await new getToken().posteve(whereUrl, whereQuery);
    if (res.data.length > 0) {
      new result(ctx, "类目已存在", 201).answer();
    } else {
      //添加菜品类目
      try {
        await new getToken().posteve(addUrl, query);
        new result(ctx, "提交成功", 200).answer();
      } catch (error) {
        new result(ctx, "提交失败", 500).answer();
      }
    }
  } catch (error) {
    new result(ctx, "fail", 500).answer();
  }
});
//获取菜品类目
router.get('/obtaincate',new Auth().m ,async ctx =>{
    const {page} = ctx.query
    let sk = page*10
    let query = `db.collection('category').orderBy('cid','desc').limit(10).skip(${sk}).get()`
    try {
        const res = await new getToken().posteve(whereUrl,query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        let total = {total : res.pager.Total }
        const arr = {...{result:data},...total}
        new result(ctx,'SUCCESS',200,arr).answer()
    } catch (error) {
        new result(ctx,'FAIL',500)
    }
})
module.exports = router.routes();
